//
//  CSCardBaseView.m
//  PresentTeahouse
//
//  Created by 吴宇 on 16/3/23.
//  Copyright © 2016年 yestae. All rights reserved.
//

#import "CSCardBaseView.h"

@implementation CSCardBaseView


- (instancetype)initWithCardFrame:(CGRect)frame
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initCSCardView:frame];
        self.hidden = YES;
    }
    
    return self;
}

- (void)dealloc
{
    
}

- (void)initCSCardView:(CGRect)cframe
{
    _bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _bgBtn.frame = self.bounds;
    _bgBtn.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    _bgBtn.alpha = 0.5;
    [_bgBtn addTarget:self action:@selector(hiddenCard) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_bgBtn];
    
    _cardView = [[UIView alloc] initWithFrame:cframe];
    _cardView.backgroundColor = [UIColor whiteColor];
    _cardView.alpha = 1.0;
    _cardView.layer.masksToBounds = YES;
    _cardView.layer.cornerRadius =9.0;
    [self addSubview:_cardView];
}

- (void)showCard
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.hidden = NO;
    
    //创建一个CABasicAnimation对象
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    _cardView.layer.anchorPoint = CGPointMake(.5,.5);
    animation.fromValue = @0.5f;
    animation.toValue = @1.0f;
    
    //动画时间
    animation.duration=0.2;
    //是否反转变为原来的属性值
    // animation.autoreverses=YES;
    //把animation添加到图层的layer中，便可以播放动画了。forKey指定要应用此动画的属性
    [_cardView.layer addAnimation:animation forKey:@"scale"];
}

- (void)hiddenCard
{
    //创建一个CABasicAnimation对象
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    _cardView.layer.anchorPoint = CGPointMake(.5,.5);
    animation.fromValue = @1.0f;
    animation.toValue = @0.5f;
    
    //动画时间
    animation.duration=0.2;
    //是否反转变为原来的属性值
    // animation.autoreverses=YES;
    //把animation添加到图层的layer中，便可以播放动画了。forKey指定要应用此动画的属性
    [_cardView.layer addAnimation:animation forKey:@"scale"];
    
    [self performSelector:@selector(hiddenSelf) withObject:nil afterDelay:0.15];
    
}

- (void)hiddenSelf
{
    self.hidden = YES;
    [self removeFromSuperview];
}

@end
